﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;


namespace Drafter.Model
{
    public class Draft
    {
        public string Title { get; set; }
        public List<Chapter> RootChapters { get; set; }

        public static void WriteToFile(String filename,Draft draft)
        {
            XmlSerializer serializer=new XmlSerializer(typeof(Draft));
            TextWriter writer = new StreamWriter(filename);
            try
            {
                serializer.Serialize(writer, draft);
            }
            finally
            {
                writer.Close();
            }
          
            
        }
        public static Draft ReadFromFile(String filename)
        {
            Draft draft = null;

            XmlSerializer serializer = new XmlSerializer(typeof(Draft));
            serializer.UnknownNode +=new XmlNodeEventHandler(serializer_UnknownNode);
            serializer.UnknownAttribute += new XmlAttributeEventHandler(serializer_UnknownAttribute);

            FileStream fs = new FileStream(filename, FileMode.Open);
            try
            {
                draft = (Draft)serializer.Deserialize(fs);
            }finally{
                fs.Close();
            }
            return draft;
        }
        protected static void serializer_UnknownNode(object sender, XmlNodeEventArgs e)
        {
            //Console.WriteLine("Unknown Node:" + e.Name + "\t" + e.Text);
            throw new NotImplementedException();
        }

        protected static void serializer_UnknownAttribute(object sender, XmlAttributeEventArgs e)
        {
            //System.Xml.XmlAttribute attr = e.Attr;
            //Console.WriteLine("Unknown attribute " +
            //attr.Name + "='" + attr.Value + "'");
            throw new NotImplementedException();
        }

    }
  

}
